﻿namespace WebBrowserWaiter
{
    using System;
    
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;

    public class MouseClick
    {
         
            
        [StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体，实际上是一种内存转换
        public struct POINTAPI
        {
            public int X;
            public int Y;
        }
 
        [DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
        public static extern int GetCursorPos(
            ref POINTAPI lpPoint
        );
 
        [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
        public static extern int WindowFromPoint(
            int xPoint,
            int yPoint
        );

        [DllImport("user32.dll", EntryPoint = "GetWindowText")]
        public static extern int GetWindowText(
            int hWnd,
            StringBuilder lpString,
            int nMaxCount
        );
            
        [DllImport("user32.dll")]       
        public extern static IntPtr GetForegroundWindow();
        
        
        public String mouseClick()
        {
            // 鼠标点击的窗口句柄
            POINTAPI point = new POINTAPI();//必须用与之相兼容的结构体，类也可以
            GetCursorPos(ref point);//获取当前鼠标坐标
            int hwnd = WindowFromPoint(point.X, point.Y);//获取指定坐标处窗口的句柄11998028 1511540
            mshtml.IHTMLDocument2 id =  new Win32API().GetHtmlDocument(hwnd);
            if (id == null)
            {
                MessageBox.Show("请使用ie 或是 360兼容模式打开");
                return "";
            }
            String idUrl = id.url;
            return id.body.outerHTML; 
        }
    }
}